﻿using java.lang;
using Microsoft.AspNetCore.Mvc;
using System.Text;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Learn.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LearnController : ControllerBase
    {

        // GET api/<LearnController>/5
        [HttpGet("{id}")]
        public void Get(int id)
        {
            // 使用String进行字符串拼接
            string str = "Hello, ";
            for (int i = 0; i < 5; i++)
            {
                str += "World"; // 每次拼接都会创建新的String对象
            }
            Console.WriteLine(str); // 输出: Hello, WorldWorldWorldWorldWorld

            // 使用StringBuilder进行字符串拼接
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("Hello, ");
            for (int i = 0; i < 5; i++)
            {
                sb.Append("World"); // 在内部字符数组中拼接，不会创建新对象
            }
            Console.WriteLine(sb.ToString()); // 输出: Hello, WorldWorldWorldWorldWorld

            // 使用StringBuffer进行字符串拼接（在多线程环境中才体现线程安全性）
            StringBuffer sbBuffer = new StringBuffer();
            sbBuffer.append("Hello, ");
            for (int i = 0; i < 5; i++)
            {
                sbBuffer.append("World"); // 在内部字符数组中拼接，同步操作
            }
            Console.WriteLine(sbBuffer.ToString()); // 输出: Hello, WorldWorldWorldWorldWorld
        }

        [HttpGet]
        public void AndAnd(int i)
        {
            if (AssertEqual(i, 1) & AssertEqual(i, 3))
            {
                Console.WriteLine("只有一个and");
            }
            if (AssertEqual(i, 1) && AssertEqual(i, 3))
            {
                Console.WriteLine("double个and");
            }

        }

        private bool AssertEqual(int i1, int i2)
        {
            return i1 == i2;
        }

        [HttpPost]
        public void Change(int a, int b)
        {
            a = a <= 0 ? 100 : a;
            b = b <= 0 ? 200 : b;
            a = a ^ b;
            b = a ^ b;
            a = a ^ b;
            Console.WriteLine("a={0};b={1}", a, b);
        }

        [HttpPost]
        public void ParentVirtual()
        {
            Son son = new();
            son.PrintFields();

            int[] arr = { 1, 2, 3, 4, 5 };
            Console.WriteLine(string.Join(",",arr));
            foreach (ref int item in arr.AsSpan())
            {
                item++;
            }
            Console.WriteLine(string.Join(",", arr));
        }

        [HttpPost]
        public void Lambda()
        {
            int y = 5;
            //=int lambda(int x) => x + y;
            Func<int, int> lambda = x => x + y;
            Console.WriteLine(lambda(1));
            y = 10;
            Console.WriteLine(lambda(1));
        }

    }

    internal class Parent
    {
        public Parent()
        {
            PrintFields();
        }

        public virtual void PrintFields() { }
    }

    internal class Son:Parent
    {
        int x = 1;
        int y;
        public Son()
        {
            y = -1;
        }
        public override void PrintFields()
        {
            Console.WriteLine("x={0},y={1}", x, y);
        }
    }

}
